<?php

namespace App;

use App\Http\Controllers\Easemob;
use App\Models\BankCard;
use App\Util\Tool;
use App\Util\Url;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 'name', 'phone', 'password','api_token','sex','age', 'updated_at',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
//        'password'
    ];

    /**
     * 获取用户推荐的用户信息
     * @return HasMany
     */
    public function getRecommends(): HasMany
    {
        return $this->hasMany('App\Models\UsersRecommend', 'referrer_id');
    }

    /***
     * @param $data
     * @return mixed
     * 修改质料
     */
    public function updateUserInfo($data)
    {
        if(isset($data['phone'])){
            $da = $this->where([['phone','=',$data['phone']]])->first();
            if(!empty($da)){
                return false;
            }
        }
        $redata = $this->getEditInfo($data['id']);
        $redata->name = isset($data['name']) ? $data['name'] : $redata->name;
        $redata->phone = isset($data['phone']) ? $data['phone'] : $redata->phone;
        $redata->image = isset($data['image']) ? $data['image'] : $redata->image;
        $redata->sex = isset($data['sex']) ? $data['sex'] : $redata->sex;
        $redata->age = isset($data['age']) ? $data['age'] : $redata->age;
        $redata->wechat = isset($data['wechat']) ? $data['wechat'] : $redata->wechat;
        $redata->ali_account = isset($data['ali_account']) ? $data['ali_account'] : $redata->ali_account;
        $redata->ali_name = isset($data['ali_name']) ? $data['ali_name'] : $redata->ali_name;
        $redata->save();
        if (isset($data['image'])) { //修改环信
            $ease = new Easemob();
            $dd['image'] = $data['image'];
            $dd['im_id'] = 'user' . config("omnipay.Im.salt") . $data['id'];
            $ease::updateUserInfo($dd);
        }
        if (isset($data['name'])) { //修改环信
            $ease = new Easemob();
            $dd['name'] = $data['name'];
            $dd['im_id'] = 'user' . config("omnipay.Im.salt") . $data['id'];
            $ease::updateUserInfo($dd);
        }
        if (isset($data['api_token'])) {
            Tool::SetAndUpToken($data['api_token'], $redata->phone);
        }
        return true;
    }

    /**
     * @param $data
     * @return mixed
     * 注销账号
     */
    public function cancelUserInfo($data)
    {
        return $this::where("id", "=", $data['user_id'])->update(['status' => 1]);
    }


    /***
     * @param $data
     * @return mixed
     * 修改质料
     */
    public function updateUserBank($data)
    {
        $redata = $this->getUserBackInfo($data['user_id']);
        if (!empty($redata)) {
            $redata->open_bank = isset($data['open_bank']) ? $data['open_bank'] : $redata->open_bank;
            $redata->province = isset($data['province']) ? $data['province'] : $redata->province;
            $redata->city = isset($data['city']) ? $data['city'] : $redata->city;
            $redata->branch = isset($data['branch']) ? $data['branch'] : $redata->branch;
            $redata->card_num = isset($data['card_num']) ? $data['card_num'] : $redata->card_num;
            $redata->card_name = isset($data['card_name']) ? $data['card_name'] : $redata->card_name;
            $redata->card_type = isset($data['card_type']) ? $data['card_type'] : $redata->card_type;
            $redata->user_id = isset($data['user_id']) ? $data['user_id'] : $redata->user_id;
            return $redata->save();
        } else {
            $banks = new BankCard();
            $banks->open_bank = isset($data['open_bank']) ? $data['open_bank'] : "";
            $banks->province = isset($data['province']) ? $data['province'] : "";
            $banks->city = isset($data['city']) ? $data['city'] : "";
            $banks->branch = isset($data['branch']) ? $data['branch'] : "";
            $banks->card_num = isset($data['card_num']) ? $data['card_num'] : "";
            $banks->card_name = isset($data['card_name']) ? $data['card_name'] : "";
            $banks->card_type = isset($data['card_type']) ? $data['card_type'] : "";
            $banks->user_id = isset($data['user_id']) ? $data['user_id'] : "";
            return $banks->save();
        }
    }

    public function getUserBackInfo($uid)
    {
        return (new BankCard())->where([['user_id', '=', $uid]])->first();
    }


    public function getInfo($id)
    {
        $host = new Url();
        $info = $this->where([['id', '=', $id]])->first();
        if(!empty($info->image)){
            $info->image = $host::getHttpHost() . 'upload/' . $info->image;
        }
        return $info;
    }

    /**
     * @param $id
     * @return mixed
     * 编辑用户信息不需要拼接域名
     */
    public function getEditInfo($id)
    {
        return $this->where([['id', '=', $id]])->first();
    }
}
